<template>
  <div class="mt-liveform">
    <slot></slot>
  </div>
</template>

<script>
  let validate = app.validate
  const liveBehavior = require('../../behaviors/live.behavior')

  export default {
    mtRelation: {
      liveBehavior: {
        type: 'descendant', // 关联的目标节点应为子孙节点
        target: liveBehavior,
      },
    },
    behaviors: [],
    data() {
      return {
        mtFormLiveValid: false,
        liveData: {},
        schema: {},
      }
    },
    methods: {
      setLiveData(d) {
        this.$set({
          liveData: d
        })
      },
      setItems(obj, keys, cb) {
        let self = this
        let data = {
          liveData: {}
        }
        keys.forEach((key) => {
          if (obj.hasOwnProperty(key)) {
            data.liveData[key] = obj[key]
          }
        })
        self.setData(data)
        self._validate(data.liveData, (errors) => {
          self.$set('mtFormLiveValid', !errors)
          if (cb) {
            cb({
              valid: !errors
            })
          }
        })
      },
      mtLiveSetItems(obj, keys) {
        let self = this
        keys.forEach((key) => {
          if (obj.hasOwnProperty(key)) {
            wx.nextTick(() => {
              self.mtLiveSetItem(key, obj[key])
            })
          }
        })
      },
      mtLiveSetItem(name, newVal, d = {}) {
        this.$set(`liveData.${name}`, newVal)
        this._validate(this.data.liveData, (errors, fields) => {
          this.$set('mtFormLiveValid', !errors)
          this.$native.emit('live-change', {
            ...d,
            name,
            newVal,
            errors,
            valid: !errors,
            liveData: this.data.liveData,
            res: fields,
          })
        })
      },
      mtLiveSet(fromTarget, newVal, oldVal, newValIsNull) {
        // if (fromTarget.hasBehavior("wx://form-field")) {
        let name = fromTarget.data.name
        if (name) {
          this.mtLiveSetItem(name, newVal, {
            fromTarget,
            oldVal,
            newValIsNull,
          })
        }
        // }
      },
      _validate(liveData, cb) {
        let schema
        if (!this.data.schema) {
          console.error('not validate schema')
        } else {
          schema = this.data.schema
        }
        if (this.getValidateSchema) {
          schema = this.getValidateSchema(liveData)
        }
        validate(liveData, schema, cb)
      },
      setValidateSchema(schema) {
        this.setData({
          schema,
        })
      },
      getValidateSchema() {

      },
      setDefaultData(d) {
        this.setData({
          liveData: d
        })
      },
      getData() {
        return this.data.liveData
      },
      validate(cb) {
        if (cb) {
          this._validate(this.data.liveData, (errors, fields) => {
            cb({
              liveData: this.data.liveData,
              valid: !errors,
              errors,
              res: fields,
            })
          })
        } else {
          console.error('mt-liveform need cb')
        }
      }
    },
  }
</script>

<style lang="less">
</style>

